﻿// -----------------------------------------------------------------------
// <copyright file="FeeData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Fee
    /// </summary>
    public class FeeData
    {
        /// <summary>
        /// Get Fee By Id
        /// </summary>
        /// <param name="feesId">Param feesId</param>
        /// <returns>Return Fee</returns>
        public FeeEN GetFeeById(int feesId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                FeeEN fee = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesGetById, feesId);
                while (reader.Read())
                {
                    fee = new FeeEN();

                    if (!reader.IsDBNull(0))
                    {
                        fee.FeesId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        fee.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        fee.ChargeUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        fee.ChargeData = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        fee.Fees = reader.GetDecimal(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        fee.Note = reader.GetString(5);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return fee;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Fee
        /// </summary>
        /// <returns>Return list all Fee</returns>
        public IList<FeeEN> GetFees()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<FeeEN> lstFee = new List<FeeEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesGetAll);
                while (reader.Read())
                {
                    FeeEN fee = new FeeEN();

                    if (!reader.IsDBNull(0))
                    {
                        fee.FeesId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        fee.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        fee.ChargeUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        fee.ChargeData = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        fee.Fees = reader.GetDecimal(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        fee.Note = reader.GetString(5);
                    }

                    lstFee.Add(fee);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstFee;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Fee
        /// </summary>
        /// <param name="fee">Paarm Fee</param>
        /// <returns>Insert Fee</returns>
        public int Insert(FeeEN fee)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ProfileId, fee.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ChargeUserId, fee.ChargeUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ChargeData, (object)fee.ChargeData ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.Fees, fee.Fees),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.Note, fee.Note)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Fee
        /// </summary>
        /// <param name="fee">Param Fee</param>
        public void Update(FeeEN fee)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Fee.FeesId, fee.FeesId),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ProfileId, fee.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ChargeUserId, fee.ChargeUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.ChargeData, (object)fee.ChargeData ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.Fees, fee.Fees),
                                    new SqlParameter(Constants.SqlParamerterName.Fee.Note, fee.Note)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Fee by feesId
        /// </summary>
        /// <param name="feesId">param feesId</param>
        public void Delete(int feesId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Fee.FeesId, feesId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list Fee By ProfileId
        /// </summary>
        /// <param name="profileId">Paarm profileId</param>
        /// <returns>Return list Fee</returns>
        public IList<FeeEN> GetFeeByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<FeeEN> lstFee = new List<FeeEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneFeesGetByProfileId, profileId);
                while (reader.Read())
                {
                    FeeEN fee = new FeeEN();

                    if (!reader.IsDBNull(0))
                    {
                        fee.FeesId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        fee.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        fee.ChargeUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        fee.ChargeData = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        fee.Fees = reader.GetDecimal(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        fee.Note = reader.GetString(5);
                    }

                    lstFee.Add(fee);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstFee;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}